package com.pn.service.impl;

import com.pn.entity.OutStore;
import com.pn.entity.Result;
import com.pn.mapper.ProductMapper;
import com.pn.page.Page;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.pn.mapper.OutStoreMapper;
import com.pn.service.OutStoreService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class OutStoreServiceImpl implements OutStoreService{

    @Resource
    private OutStoreMapper outStoreMapper;
    @Resource
    private ProductMapper productMapper;

    @Override
    public int addOutStore(OutStore outStore) {
        return outStoreMapper.insertOutStore(outStore);
    }

    @Override
    public Page findOutStoreByPage(OutStore outStore, Page page) {
        int result = outStoreMapper.countOutStore(outStore);
        page.setTotalNum(result);
        List<OutStore> outStores = outStoreMapper.selectOutStoreByPage(outStore, page);
        page.setResultList(outStores);
        return page;
    }

    @Transactional
    @Override
    public Result modifyIsOut(OutStore outStore,Integer updateBy) {
        int invent = productMapper.selectInventByPid(outStore.getProductId());
        if (invent < outStore.getOutNum()){
            return Result.err(Result.CODE_ERR_BUSINESS,"库存不足！");
        }
        int result = outStoreMapper.updateIsOutByOutsId(outStore.getOutsId());
        if (result > 0){
            result = productMapper.updateInventByPid(outStore.getProductId(), outStore.getOutNum() * -1, updateBy);
            if (result > 0){
                return Result.ok("确认入库成功！");
            }
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"确认入库失败！");
    }
}
